<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * DISPENDA2BPD WEBSERVICE
 *
 * An close source application development
 *
 * @package		DISPENDA GIANYAR WEBSERVICE
 * @author		Andi Kurniawan
 * @copyright           PT. RUMAH LINUX YOGYAKARTA (c) 2013.
 * @license		nakamichikun@gmail.com
 * @follow		@nakamichikun
 * @since		Version 1.0
 * @filesource
 */
class Webservice extends CI_Controller {

    function __construct() {
        parent::__construct();
        set_time_limit(0);
        ini_set('memory_limit', '500M');
        ini_set('default_socket_timeout', 1000000);
        $this->load->model('modsvc');
    }

    function index() {
        $this->load->view('welcome');
    }

    function echoTest() {
        $ser_params = array(
            'echo_test' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            $result = $client->__soapCall('echo_test', $ser_params);
            $structur = $result->echo_testResult->any;
            $simple = simplexml_load_string($structur);
            $status = $simple->NewDataSet->Table1;
            echo '<pre>';
            print_r($status);
            echo '</pre>';
        } catch (Exception $ex) {
            echo $ex->faultstring;
        }
    }

    public function inquery($nop) {
        $ser_params = array(
            'inquery' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
                'sInstansi' => INS_WS,
                'sNoId' => $nop, //nop di inputkan oleh user
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            try {
                $result = $client->__soapCall('inquery', $ser_params);
                $structur = $result->inqueryResult->any;
                $simple = simplexml_load_string($structur);
                $new = $simple->NewDataSet;
                return $new;
            } catch (Exception $ex) {
                echo '<pre>';
                echo $ex->faultstring;
                echo '</pre>';
            }
        } catch (Exception $ex) {
            echo '<pre>';
            echo $ex->faultstring;
            echo '</pre>';
        }
    }

    function laporanPaymentDetail() {
        $ser_params = array(
            'laporan_payment_detail' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
                'sInstansi' => INS_WS,
                'sDate' => HR
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            $result = $client->__soapCall('laporan_payment_detail', $ser_params);
            $xm = $result->laporan_payment_detailResult->any;
            $obj = simplexml_load_string($xm);
            $ro = $obj->NewDataSet->Table;
            foreach ($ro as $value) {
                $id = $value->id;
                $sts_reversal = $value->sts_reversal;
                $chk = $this->modsvc->chkTrxBank($id, $sts_reversal);
                if ($chk == TRUE) {
                    $qryIns = "INSERT INTO TRANSAKSI_BANK(ID,NO_ID,TAGIHAN,TAHUN,TGL_TX,KD_CAB,KD_USER,STS_REVERSAL) VALUES('" . $value->id . "','" . $value->no_id . "'," . $value->tagihan . ",'" . $value->Tahun . "','" . $value->tgl_tx . "','" . $value->kd_cab . "','" . $value->kd_user . "','" . $value->sts_reversal . "')";
//                    echo $qryIns . '<br/>';
                    $this->modsvc->umum($qryIns);
                    $enc = json_encode(str_replace("'", "", $qryIns));
                    $this->modsvc->logRptPay($enc);
                    $rf = $value->no_id;
                    $KD_PROPINSI = substr($rf, 0, 2);
                    $KD_DATI2 = substr($rf, 2, 2);
                    $KD_KECAMATAN = substr($rf, 4, 3);
                    $KD_KELURAHAN = substr($rf, 7, 3);
                    $KD_BLOK = substr($rf, 10, 3);
                    $NO_URUT = substr($rf, 13, 4);
                    $KD_JNS_OP = substr($rf, 17, 1);
                    $THN_PAJAK_SPPT = $value->Tahun;
                    $PEMBAYARAN_SPPT_KE = 1;
                    $KD_KANWIL_BANK = $KD_PROPINSI;
                    $KD_KPPBB_BANK = '-';
                    $KD_BANK_TUNGGAL = '-';
                    $KD_BANK_PERSEPSI = '-'; //No transaksi dari bank
                    $KD_TP = '-';
                    $DENDA_SPPT = $value->Denda;
                    $JML_SPPT_YG_DIBAYAR = $value->tagihan + $value->Denda;
                    $TGL_PEMBAYARAN_SPPT = substr($value->tgl_tx, 0, 10);
                    $TGL_REKAM_BYR_SPPT = gmdate('Y-m-d');
                    $NIP_REKAM_BYR_SPPT = $value->kd_user;
                    $ID_TRANSAKSI = $value->id;
                    $query = "INSERT INTO PEMBAYARAN_SPPT_BANK(
                KD_PROPINSI,
                KD_DATI2,
                KD_KECAMATAN,
                KD_KELURAHAN,
                KD_BLOK,
                NO_URUT,
                KD_JNS_OP,
                THN_PAJAK_SPPT,
                PEMBAYARAN_SPPT_KE,
                KD_KANWIL_BANK,
                KD_KPPBB_BANK,
                KD_BANK_TUNGGAL,
                KD_BANK_PERSEPSI,
                KD_TP,
                DENDA_SPPT,
                JML_SPPT_YG_DIBAYAR,
                TGL_PEMBAYARAN_SPPT,
                TGL_REKAM_BYR_SPPT,
                NIP_REKAM_BYR_SPPT,
                ID_TRANSAKSI_BANK
                )
                VALUES('"
                            . $KD_PROPINSI . "','"
                            . $KD_DATI2 . "','"
                            . $KD_KECAMATAN . "','"
                            . $KD_KELURAHAN . "','"
                            . $KD_BLOK . "','"
                            . $NO_URUT . "','"
                            . $KD_JNS_OP . "','"
                            . $THN_PAJAK_SPPT . "','"
                            . $PEMBAYARAN_SPPT_KE . "','"
                            . $KD_KANWIL_BANK . "','"
                            . $KD_KPPBB_BANK . "','"
                            . $KD_BANK_TUNGGAL . "','"
                            . $KD_BANK_PERSEPSI . "','"
                            . $KD_TP . "',"
                            . $DENDA_SPPT . ","
                            . $JML_SPPT_YG_DIBAYAR . ",TO_DATE('"
                            . $TGL_PEMBAYARAN_SPPT . "','yyyy-MM-DD'),TO_DATE('"
                            . $TGL_REKAM_BYR_SPPT . "','yyyy-MM-DD'),'"
                            . $NIP_REKAM_BYR_SPPT . "','"
                            . $ID_TRANSAKSI
                            . "')";
                    $this->modsvc->reportPayment($query);
                }
                if (!isset($query)) {
                    $qry = 'Data sudah diinput sebelumnya.';
                } else {
                    $qry = $query;
                }
                $enco = json_encode(str_replace("'", "", $qry));
                $this->modsvc->logRptPay($enco);
            }
//            echo '<pre>';
//            print_r($obj);
//            echo '</pre>';
            return $ro;
        } catch (Exception $ex) {
            echo $ex->faultstring;
        }
    }

    function laporanPaymentDetailManual($date) {
        $ser_params = array(
            'laporan_payment_detail' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
                'sInstansi' => INS_WS,
                'sDate' => $date
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            $result = $client->__soapCall('laporan_payment_detail', $ser_params);
            $xm = $result->laporan_payment_detailResult->any;
            $obj = simplexml_load_string($xm);
            $ro = $obj->NewDataSet->Table;
            foreach ($ro as $value) {
                $id = $value->id;
                $sts_reversal = $value->sts_reversal;
                $chk = $this->modsvc->chkTrxBank($id, $sts_reversal);
                if ($chk == TRUE) {
                    $qryIns = "INSERT INTO TRANSAKSI_BANK(ID,NO_ID,TAGIHAN,TAHUN,TGL_TX,KD_CAB,KD_USER,STS_REVERSAL) VALUES('" . $value->id . "','" . $value->no_id . "'," . $value->tagihan . ",'" . $value->Tahun . "','" . $value->tgl_tx . "','" . $value->kd_cab . "','" . $value->kd_user . "','" . $value->sts_reversal . "')";
//                    echo $qryIns . '<br/>';
                    $this->modsvc->umum($qryIns);
                    $enc = json_encode(str_replace("'", "", $qryIns));
                    $this->modsvc->logRptPay($enc);
                    $rf = $value->no_id;
                    $KD_PROPINSI = substr($rf, 0, 2);
                    $KD_DATI2 = substr($rf, 2, 2);
                    $KD_KECAMATAN = substr($rf, 4, 3);
                    $KD_KELURAHAN = substr($rf, 7, 3);
                    $KD_BLOK = substr($rf, 10, 3);
                    $NO_URUT = substr($rf, 13, 4);
                    $KD_JNS_OP = substr($rf, 17, 1);
                    $THN_PAJAK_SPPT = $value->Tahun;
                    $PEMBAYARAN_SPPT_KE = 1;
                    $KD_KANWIL_BANK = $KD_PROPINSI;
                    $KD_KPPBB_BANK = '-';
                    $KD_BANK_TUNGGAL = '-';
                    $KD_BANK_PERSEPSI = '-'; //No transaksi dari bank
                    $KD_TP = '-';
                    $DENDA_SPPT = $value->Denda;
                    $JML_SPPT_YG_DIBAYAR = $value->tagihan + $value->Denda;
                    $TGL_PEMBAYARAN_SPPT = substr($value->tgl_tx, 0, 10);
                    $TGL_REKAM_BYR_SPPT = gmdate('Y-m-d');
                    $NIP_REKAM_BYR_SPPT = $value->kd_user;
                    $ID_TRANSAKSI = $value->id;
                    $query = "INSERT INTO PEMBAYARAN_SPPT_BANK(
                KD_PROPINSI,
                KD_DATI2,
                KD_KECAMATAN,
                KD_KELURAHAN,
                KD_BLOK,
                NO_URUT,
                KD_JNS_OP,
                THN_PAJAK_SPPT,
                PEMBAYARAN_SPPT_KE,
                KD_KANWIL_BANK,
                KD_KPPBB_BANK,
                KD_BANK_TUNGGAL,
                KD_BANK_PERSEPSI,
                KD_TP,
                DENDA_SPPT,
                JML_SPPT_YG_DIBAYAR,
                TGL_PEMBAYARAN_SPPT,
                TGL_REKAM_BYR_SPPT,
                NIP_REKAM_BYR_SPPT,
                ID_TRANSAKSI_BANK
                )
                VALUES('"
                            . $KD_PROPINSI . "','"
                            . $KD_DATI2 . "','"
                            . $KD_KECAMATAN . "','"
                            . $KD_KELURAHAN . "','"
                            . $KD_BLOK . "','"
                            . $NO_URUT . "','"
                            . $KD_JNS_OP . "','"
                            . $THN_PAJAK_SPPT . "','"
                            . $PEMBAYARAN_SPPT_KE . "','"
                            . $KD_KANWIL_BANK . "','"
                            . $KD_KPPBB_BANK . "','"
                            . $KD_BANK_TUNGGAL . "','"
                            . $KD_BANK_PERSEPSI . "','"
                            . $KD_TP . "',"
                            . $DENDA_SPPT . ","
                            . $JML_SPPT_YG_DIBAYAR . ",TO_DATE('"
                            . $TGL_PEMBAYARAN_SPPT . "','yyyy-MM-DD'),TO_DATE('"
                            . $TGL_REKAM_BYR_SPPT . "','yyyy-MM-DD'),'"
                            . $NIP_REKAM_BYR_SPPT . "','"
                            . $ID_TRANSAKSI
                            . "')";
                    $this->modsvc->reportPayment($query);
                }
                if (!isset($query)) {
                    $qry = 'Data sudah diinput sebelumnya.';
                } else {
                    $qry = $query;
                }
                $enco = json_encode(str_replace("'", "", $qry));
                $this->modsvc->logRptPay($enco);
            }
//            echo '<pre>';
//            print_r($obj);
//            echo '</pre>';
            return $ro;
        } catch (Exception $ex) {
            echo $ex->faultstring;
        }
    }

    function tagihanDeleteById($tib) {
        $ser_params = array(
            'tagihan_delete_by_id' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
                'sInstansi' => INS_WS,
                'sNoid' => $tib //nop di inputkan oleh user
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            $result = $client->__soapCall('tagihan_delete_by_id', $ser_params);
            $structur = $result->tagihan_delete_by_idResult->any;
            $simple = simplexml_load_string($structur);
            $new = $simple->NewDataSet;
            $tbl = $new->Table1;
            return $tbl;
        } catch (Exception $ex) {
            echo $ex->faultstring;
        }
    }

    function tagihanDeleteInstansi() {
        $params = array(
            'tagihan_delete_instansi' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
                'sInstansi' => INS_WS
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            $back = $client->__soapCall('tagihan_delete_instansi', $params);
            $structur = $back->tagihan_delete_instansiResult->any;
            $simple = simplexml_load_string($structur);
            $status = $simple->NewDataSet->Table1;
//            $status_code = $status->code;
            $enc = json_encode($params);
            $this->modsvc->logDelIns($enc, json_encode($status));
            return $status;
        } catch (Exception $ex) {
            echo $ex->faultstring;
        }
    }

    function tagihanInsert($data) {
        $tos = array(
            'tagihan_insert' => array(
                'sUser' => USR_WS,
                'sPassword' => PSWD_WS,
                'sInstansi' => INS_WS,
                'sNoid' => $data->id,
                'sNama' => $data->nama,
                'jum_tagihan' => $data->jumlah,
                'sKet_1' => $data->nop
            )
        );
        try {
            $client = new SoapClient(URL_WS);
            $back = $client->__soapCall('tagihan_insert', $tos);
            $enc = json_encode($tos);
            $structur = $back->tagihan_insertResult->any;
            $simple = simplexml_load_string($structur);
            $status = $simple->NewDataSet->Table1;
            $encSts = json_encode($status);
            $status_code = $status->code;
            $this->modsvc->logIns($enc, $encSts);
            if ($status_code == '00') {
                $this->modsvc->insSpptKirim($data);
            }
            return $status;
        } catch (Exception $ex) {
            echo $ex->faultstring;
        }
    }

}
